package com.pine.admin.modules.system.service.impl;

import com.pine.admin.modules.system.dao.SysPermissionDao;
import com.pine.admin.modules.system.entity.ActiveUserEntity;
import com.pine.admin.modules.system.entity.SysPermissionEntity;
import com.pine.admin.modules.system.service.SysPermissionService;
import org.springframework.beans.factory.annotation.Autowired;



import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;
import com.pine.common.dto.Page;
import org.springframework.stereotype.Service;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


@Service("sysPermissionService")
@Slf4j
public class SysPermissionServiceImpl implements SysPermissionService {

    @Autowired
    private SysPermissionDao sysPermissionDao;


    @Override
    public List<SysPermissionEntity> getSysPermissionList(SysPermissionEntity record) {

        log.info("getSysPermissionList");
        try {

            return sysPermissionDao.queryAllSysPermission(record);
        } catch (Exception e) {
            log.error("getSysPermissionList", e);
        }

        return null;
    }

    @Override
    public List<SysPermissionEntity> getSysPermissionByPage(Page page, SysPermissionEntity record) {

        log.info("getSysPermissionByPage");
        try {

            Map<String, Object> parameter = new HashMap<String, Object>();
            parameter.put("record", record);
            parameter.put("page", page);

            return sysPermissionDao.querySysPermissionsByPage(parameter);
        } catch (Exception e) {
            log.error("getSysPermissionByPage", e);
        }

        return null;
    }

    @Override
    public SysPermissionEntity getSysPermissionById(String id) {

        log.info("getSysPermissionById");
        try {

            return sysPermissionDao.selectByPrimaryKey(Integer.valueOf(id));
        } catch (Exception e) {
            log.error("getSysPermissionById", e);
        }

        return null;
    }

    @Override
    public SysPermissionEntity getSysPermission(SysPermissionEntity record) {

        log.info("getSysPermission");
        try {

            return sysPermissionDao.querySysPermission(record);
        } catch (Exception e) {
            log.error("getSysPermission", e);
        }

        return null;
    }

    @Override
    public boolean createSysPermission(SysPermissionEntity record) {

        log.info("createSysPermission");
        try {

            int updates = sysPermissionDao.insertSelective(record);

            if (updates > 0) {
                return true;
            }
        } catch (Exception e) {
            log.error("createSysPermission", e);
        }

        return false;
    }

    @Override
    public boolean deleteSysPermission(String id) {

        log.info("deleteSysPermission");
        try {

            int updates = sysPermissionDao.delete(Integer.valueOf(id));

            if (updates > 0) {
                return true;
            }
        } catch (Exception e) {
            log.error("deleteSysPermission", e);
        }

        return false;
    }

    @Override
    public boolean updateSysPermission(SysPermissionEntity record) {

        log.info("updateSysPermission");
        try {

            int updates = sysPermissionDao.updateByPrimaryKeySelective(record);

            if (updates > 0) {
                return true;
            }
        } catch (Exception e) {
            log.error("updateSysPermission", e);
        }

        return false;
    }

    @Override
    public List<SysPermissionEntity> getSysMenu(HttpServletRequest request, HttpServletResponse response) {
        log.info("getSysMenu");
        try {

            //判断用户身份在Session中是否存在
            HttpSession session = request.getSession();
            ActiveUserEntity activeUser = (ActiveUserEntity) session.getAttribute("activeUser");
            //从Session中取出菜单范围的URL
            List<SysPermissionEntity> menus = activeUser.getMenus();

            List<SysPermissionEntity> sysMenus = new ArrayList<>();

            for (SysPermissionEntity sysPermission:menus){
                if( sysPermission.getParentid()==0){
                    sysMenus.add(sysPermission);
                }
            }

            for (SysPermissionEntity sysPermission:sysMenus){
                List<SysPermissionEntity> son = new ArrayList<>();
                for (SysPermissionEntity sySonPermission:menus){
                    if (sysPermission.getId().equals(sySonPermission.getParentid()) ){
                        son.add(sySonPermission);
                    }
                }
                sysPermission.setSysPermissions(son);
            }

            return sysMenus;
        } catch (Exception e) {
            log.info("getSysMenu");
        }
        return null;
    }


}
